#!/bin/sh

# This script launches the NH3D model at the given grid of processes

# If no arguments are passed to the script, the grid of processes is set to 
# nrocx = 1, nprocy = 1, maxtime is set to default value

nprocx_default=1
nprocy_default=1
maxtime_default=10
numproc_pernode_default=-1 # Default value means no special distribution per node
launch=mpirun # Chebyshev
#launch=srun #Lomonosov - real time
#launch=sbatch #Lomonosov - batch
#script=ompi #Lomonosov - Open MPI
#script=mvapich #Lomonosov - MVAPICH
#script=#run_victor #Lomonosov
#debug_key=-gdb # For Intel MPI + GNU debugger (GDB)
#debug_key=-tv # For Intel MPI + GNU debugger (GDB), mvs50k
spec_1proc_key= #"-as single"
#key_test="-q test"
key_trace= #"-trace"
key_intel_mpi="-as intel"
key_numproc="-np " #Chebyshev
#key_numproc="-n" #Lomonosov
key_maxtime="-maxtime" # Chebyshev
#key_maxtime="--time" #Lomonosov

nprocx_arg=$1
nprocy_arg=$2
maxtime_arg=$3
numproc_pernode_arg=$4
nprocx=${nprocx_arg:=$nprocx_default}
nprocy=${nprocy_arg:=$nprocy_default}
maxtime=${maxtime_arg:=$maxtime_default}
numproc=`expr $nprocx \* $nprocy`

numproc_pernode=${numproc_pernode_arg:=$numproc_pernode_default}
if [ $numproc_pernode >= 1 ]
then
  numproc_pernode_key="-t ppn="$numproc_pernode
fi

echo "Launching the NH3D model on the grid of processes nprocx = $nprocx, nprocy = $nprocy \
with $numproc_pernode processes per node, timelimit = $maxtime minutes"

if [ $numproc = "1" ]
then
  command="$launch $key_intel_mpi $key_numproc$numproc $key_test $spec_1proc_key $key_maxtime $maxtime $key_trace $script ./nh3d.out $nprocx $nprocy"
else
  command="$launch $key_intel_mpi $key_numproc$numproc $key_test $numproc_pernode_key $key_maxtime $maxtime $key_trace $script ./nh3d.out $nprocx $nprocy"
fi

echo $command
$command
